package com.zzyl.controller;

import com.zzyl.base.ResponseResult;
import com.zzyl.constant.CacheConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/resource")
public class ResourceController {

    private final ResourceService resourceService;

    /**
     * 查询所有资源
     * @param resourceDto
     * @return
     */
    @Cacheable(cacheNames = CacheConstant.RESOURCE_LIST,key = "#resourceDto.hashCode()")
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> getList(@RequestBody ResourceDto resourceDto){
        log.info("执行了查询所有资源的方法");
        List<ResourceVo> list = resourceService.getList(resourceDto);
        return ResponseResult.success(list);
    }

    /**
     * 增加菜单配置的上级菜单树形展示
     * @param resourceDto
     * @return
     */
    @Cacheable(cacheNames = CacheConstant.RESOURCE_TREE,key = "#resourceDto.hashCode()")
    @PostMapping("/tree")
    public ResponseResult<TreeVo> getTree(@RequestBody ResourceDto resourceDto){
        TreeVo treeVo = resourceService.getTree(resourceDto);
        return ResponseResult.success(treeVo);
    }

    /**
     * 新增资源
     * @param resourceDto
     * @return
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,allEntries = true),
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,allEntries = true)
            }
    )
    @PutMapping
    public ResponseResult insertResource(@RequestBody ResourceDto resourceDto){
        resourceService.insertResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 修改资源
     * @param resourceDto
     * @return
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,allEntries = true),
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,allEntries = true)
            }
    )
    @PatchMapping
    public ResponseResult update(@RequestBody ResourceDto resourceDto){
        resourceService.update(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 启用禁用
     * @param resourceDto
     * @return
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,allEntries = true),
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,allEntries = true)
            }
    )
    @PostMapping("/enable")
    public ResponseResult updateState(@RequestBody ResourceDto resourceDto){
        resourceService.updateState(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 删除资源
     * @param menuId
     * @return
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,allEntries = true),
                    @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,allEntries = true)
            }
    )
    @DeleteMapping("/{menuId}")
    public ResponseResult<List<Resource>> deleteByResourceId(@PathVariable String menuId){
        List<Resource> list = resourceService.deleteByResourceId(menuId);
        return ResponseResult.success(list);
    }
}
